﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using DQS.Core.Log;
using DQS.EL;
using DQS.BLL;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) { }

    protected void LoginIn_Click(object sender, EventArgs e)
    {
        DQ_USERS user = new DQ_USERS();
        user.USER_LOGIN = User_Login.Text.Trim();
        user.USER_PASS = Tool.EncryptPasswordByMD5(User_Pass.Text.Trim());        
        Session["LoginUser"] = DQ_USERSManager.ValidUser(user);
        if (Session["LoginUser"] == null)
        {
            LoginFail();
        }
        else
        {
            LoginSuccess();
        }
        User_Pass.Text = String.Empty;
    }

    private void LoginSuccess()
    {
        if (User_Pass.Text.Trim().Equals(DQS_Page.DefaultPass))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "resetpass"
                , "$(function(){$('#ChangePass').dialog('open');});", true);
        }
        else
        {
            Log.Instance(this).Info("{ClientIP:'" + Tool.GetClientIP() + "',Msg:'"
                + User_Login.Text.Trim() + " login in.'}");
            Response.Redirect("~/Operate/Index.aspx");
        }
    }

    private void LoginFail()
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "logfail"
            , @"$(function(){alert('用户名或者密码有误！\n注意:密码区分大小写。');});", true);
        User_Pass.Focus();
        Log.Instance(this).Info("{ClientIP:'" + Tool.GetClientIP() + "',Msg:'"
            + User_Login.Text.Trim() + " login failed.'}");
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        DQ_USERS user = (DQ_USERS)Session["LoginUser"];
        user.USER_PASS = Tool.EncryptPasswordByMD5(User_Pass_R.Text.Trim());
        DQ_USERSManager.ChangePassword(user);
        User_Pass_R.Text = "";
        User_Pass_RT.Text = "";
        Page.ClientScript.RegisterStartupScript(GetType(), "resetAlert"
            , "$(function(){alert('密码修改成功，请使用新密码重新登录系统！');});", true);
        User_Pass.Focus();
    }
}
